package com.hwhhhh.mygrad.utils;

import android.content.Context;
import android.util.TypedValue;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.hwhhhh.mygrad.entity.RespData;
import com.hwhhhh.mygrad.entity.RespDataArr;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class StringUtils {
    private static String[] JXQ = new String[] {"全\n部", "教\n一", "教\n二", "教\n三"};
    private static String[] JXQ_S = new String[] {"教一", "教二", "教三"};
    private static String[] XY = new String[] {"全\n部", "教\n一", "教\n二", "教\n三", "教\n四", "教\n五", "教\n六", "教\n七"};
    private static String[] XY_S = new String[] {"教一", "教二", "教三", "教四", "教五", "教六", "教七"};
    public static boolean isEmpty(String str) {
        return str == null || str.length() <= 0;
    }

    public static <T> T fromJson(String res, Class<T> cls) {
        Gson gson = new Gson();
        return gson.fromJson(res, cls);
    }

    // 这里type不能为泛型T， 因为其不能真正转型，java为伪泛型
    public static <T> RespData<T> fromJson(String res, Type type) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.fromJson(res, type);
    }

    // 这里type不能为泛型T， 因为其不能真正转型，java为伪泛型
    public static <T> RespDataArr<T> fromJsonArr(String res, Type type) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.fromJson(res, type);
    }

    public static String[] getSchoolBuilding(int campus, int type) {
        if (type == 0) {
            if (campus == 1 || campus == 2) return JXQ;
            else if (campus == 0) return XY;
            return JXQ;
        } else {
            if (campus == 1 || campus == 2) return JXQ_S;
            else if (campus == 0) return XY_S;
            return JXQ_S;
        }
    }

    public static String dateFormat(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.getDefault());
        return format.format(date);
    }

    public static int dpToInt(Context context, float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,context.getResources().getDisplayMetrics());
    }

    public static String getStrCls(String cls) {
        if (isEmpty(cls)) return null;
        String res = "";
        char[] chs = cls.toCharArray();
        char begin = chs[0], end = chs[0], pre = chs[0];
        // 遍历表示空闲课时的字符串
        // 具体逻辑：如果abc时输出第1~3节课，abd时输出第1~2、3节课
        for (int i = 1; i < chs.length; i++) {
            int cur = chs[i];
            // 如果当前的字符为前一个字符的下一个，则连续课时的end增加
            // 如果不是，则表示连续课时终止，输出结果。
            if (cur != pre + 1) {
                if (isEmpty(res)) res = clsChToStr(begin, end);
                else res = res + "、" + clsChToStr(begin, end);
                begin = chs[i];
            }
            // end、pre变为当前
            end = chs[i];
            pre = chs[i];
        }
        // 最后需要输出一次
        if (isEmpty(res)) res = clsChToStr(begin, end);
        else res = res + "、" + clsChToStr(begin, end);
        return "第" + res + "节课";
    }

    private static String clsChToStr(char c1, char c2){
        int begin = clsChToInt(c1);
        int end = clsChToInt(c2);
        if (begin == end) return String.valueOf(begin);
        else return begin + "~" + end;
    }

    public static int clsChToInt(char c) {
        int res;
        switch (c) {
            case 'a':
                res = 1;
                break;
            case 'b':
                res = 2;
                break;
            case 'c':
                res = 3;
                break;
            case 'd':
                res = 4;
                break;
            case 'e':
                res = 5;
                break;
            case 'f':
                res = 6;
                break;
            case 'g':
                res = 7;
                break;
            case 'h':
                res = 8;
                break;
            case 'i':
                res = 9;
                break;
            case 'j':
                res = 10;
                break;
            case 'k':
                res = 11;
                break;
            case 'l':
                res = 12;
                break;
            case 'm':
                res = 13;
                break;
            default:
                res = 13;
        }
        return res;
    }
}
